package sg.edu.nus.iss.umpire.email;


import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.persistence.EntityManager;




import sg.edu.nus.iss.umpire.dao.jpa.UmpireDAOImpl;
import sg.edu.nus.iss.umpire.domain.Competition;
import sg.edu.nus.iss.umpire.domain.CompetitionDay;
import sg.edu.nus.iss.umpire.domain.Session;
import sg.edu.nus.iss.umpire.domain.Umpire;
import sg.edu.nus.iss.umpire.support.HttpMethod;
import sg.edu.nus.iss.umpire.support.Service;
import sg.edu.nus.iss.umpire.support.ServiceConfig;
import sg.edu.nus.iss.umpire.support.ServiceContext;

@SuppressWarnings("serial")
@ServiceConfig(path = "sendConfirmEmail", method = HttpMethod.POST)
public class SendConfirmEmailServlet implements Service {

	private static Logger log = Logger.getLogger(SendConfirmEmailServlet.class.getCanonicalName());
	
	@Override
	public String serve(ServiceContext ctx) throws Exception {
		
		Long competitionId = Long.valueOf(ctx.getRequest().getParameter("competitionId"));

		Competition competition = ctx.getEntityManager().find(
				Competition.class, competitionId);
		
		sendConfirmEmailToUmpire(competition,ctx.getEntityManager(),ctx);

		return null;
	}
	
//	private String buildTimeSlots(String competitionDate, String timeSlots)
//			throws IOException
//	{
//		return ",(" + competitionDate + "," + timeSlots + ")";
//	}
	
	private void sendConfirmEmailToUmpire(Competition comp,EntityManager em, ServiceContext ctx)
			throws IOException
	{
	    HashSet<Long> confirmedUmpiresId = new HashSet<>();
	    for(CompetitionDay day : comp.getCompetitionDays()){
		for(Session session : day.getSessions()){
		    for(Long umpId : session.getConfirmedUmpire()){
			confirmedUmpiresId.add(umpId);
		    }
		}
	    }
	    ctx.commitCurrentTransaction();
	    
	    System.out.println(confirmedUmpiresId.toString());
	  
		  String subject = "Competition confirm";

		  
		  String msgBody = createConfirmEmail(comp,em,ctx);
		  
		  // send email to umpires
		  MailService mailService = new MailService();
		  em = ctx.getEntityManager();
		  for(Long umpId : confirmedUmpiresId){
		      UmpireDAOImpl umpDao = new UmpireDAOImpl(em);
		      Umpire ump = umpDao.getByKey(umpId);
		      mailService.send(ump.getEmail(), subject, msgBody);
		  }
	}
	
	private String createConfirmEmail(Competition comp, EntityManager em, ServiceContext ctx){
	    String nl = System.getProperty("line.separator");
	    em=ctx.getEntityManager();
	    String slots="";
	    for(CompetitionDay day : comp.getCompetitionDays()){
		if(day.getSessions().size()==0)
		    continue;
		for(Session session : day.getSessions()){
		    slots+=day.getDate() +" " +session.getSlotName()+": "+nl;
		    for(Long umpId : session.getConfirmedUmpire()){
			UmpireDAOImpl umpDao = new UmpireDAOImpl(em);
			Umpire ump = umpDao.getByKey(umpId);
			slots+=" - " + ump.getName()+nl;
		    }
		    slots+=nl;
		}
		slots+=nl;
	    }
	    ctx.commitCurrentTransaction();
	    
	    String msg = "Dear Umpires,"+nl+nl+
		    "Thanks for registering your interest to umpire in %s " +nl+
		    "The selected umpires for %s is as such: " +nl+nl+
		    "%s"+nl+nl+
		    "Please report to %s on time for the competition."+nl+nl+
		    "Thank you.";
	    msg = String.format(msg, comp.getName(),comp.getName(),slots,comp.getLocation());
	    return msg;
	}
}
